use std::env;
use std::fs;
use std::path::Path;
use std::process::Command;

fn main() {
    let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
    println!("cargo:warning=Target OS: {}", target_os);
    #[cfg(any())]
    if target_os == "android" {
        println!("cargo:warning=Building for Android...");

        // 设置 Termux 二进制目录
        let termux_bin = "/data/data/com.termux/files/home/bin";

        // 获取编译输出目录
        let out_dir = env::var("OUT_DIR").unwrap();
        let target_dir = Path::new(&out_dir).ancestors().nth(3).unwrap();

        // 复制可执行文件到 Termux
        for bin in &["auto_video", "auto_manga"] {
            let src = target_dir.join(bin);
            let dest = format!("{}/{}", termux_bin, bin);

            println!("cargo:warning=Copying {} to {}", bin, dest);

            // 复制文件
            Command::new("adb")
                .args(&["push", src.to_str().unwrap(), &dest])
                .status()
                .expect("Failed to push binary to device");

            // 设置执行权限
            Command::new("adb")
                .args(&["shell", "chmod", "+x", &dest])
                .status()
                .expect("Failed to set executable permission");
        }
    }
}
